package com.gict.antifraudback.controller.web.magnet;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gict.antifraudback.entity.MagnetFile;
import com.gict.antifraudback.exception.MyException;
import com.gict.antifraudback.result.ResultEnum;
import com.gict.antifraudback.result.ResultView;
import com.gict.antifraudback.service.MagnetFileService;
import com.gict.antifraudback.utils.magnet.IMagnet;
import com.gict.antifraudback.utils.magnet.MagnetFactory;
import com.gict.antifraudback.vo.MagnetFileVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ThinkPad
 */
@RestController
@Slf4j
@RequestMapping("/wapi/magnetFile")
public class MagnetFileController {

    @Autowired
    private MagnetFileService magnetFileService;

    @GetMapping("/getList")
    public ResultView getList(MagnetFileVo magnetFileVo) {
        LambdaQueryWrapper<MagnetFile> wrapper = Wrappers.lambdaQuery();
        wrapper.like(!ObjectUtils.isEmpty(magnetFileVo.getName()), MagnetFile::getName, magnetFileVo.getName());
        return ResultView.ok(magnetFileService.page(new Page<>(magnetFileVo.getCurrent(), magnetFileVo.getPageSize()), wrapper));
    }

    @GetMapping("/get")
    public ResultView get(@Valid @NotNull Integer id) {
        MagnetFile magnetFile = magnetFileService.getById(id);
        if (magnetFile == null) {
            throw new MyException("磁力不存在");
        }
        return ResultView.ok(magnetFile);
    }

    @PostMapping("/delete")
    public ResultView delete(@RequestBody ArrayList<Integer> ids) {
        log.info("magnetFileVo:{}", ids);
        for (Integer id : ids) {
            magnetFileService.removeById(id);
        }
        return ResultView.okMsg("删除成功");
    }

    /**
     * 获取所有磁力
     */
    @GetMapping("/getAllMagnet")
    public ResultView getAllMagnet() {
        List<Map<String, String>> result = new ArrayList<>();
        Set<String> magnetSource = MagnetFactory.allMagnetSource();
        for (String name : magnetSource) {
            Map<String, String> res = new HashMap<>();
            res.put("id", name);
            res.put("name", name);
            result.add(res);
        }
        return ResultView.ok(result);
    }

    /**
     * 抓取磁力
     */
    @PostMapping("/grabMagnet")
    public ResultView add(@Valid @RequestBody MagnetFileVo magnetFileVo) {
        IMagnet magnet = MagnetFactory.getSourceService(magnetFileVo.getSource());
        List<MagnetFile> magnetFileList = magnet.listMagnetByKeyWord(magnetFileVo.getName(), magnetFileVo.getPage());
/*        if (!CollectionUtils.isEmpty(magnetFileList)) {
            // 过滤已经存在的
            List<String> links = magnetFileList.stream().map(MagnetFile::getMagnetLink).collect(Collectors.toList());
            LambdaQueryWrapper<MagnetFile> wrapper = Wrappers.lambdaQuery();
            wrapper.in(MagnetFile::getMagnetLink, links);
            List<MagnetFile> list = magnetFileService.list(wrapper);

            Map<String, MagnetFile> magnetFileMap = magnetFileList.stream().collect(Collectors.toMap(MagnetFile::getMagnetLink, a -> a, (a, b) -> a));

            if (!list.isEmpty()) {
                for (MagnetFile magnetFile : list) {
                    magnetFileMap.remove(magnetFile.getMagnetLink());
                }
            }
            if (!magnetFileMap.isEmpty()) {
                magnetFileService.saveBatch(magnetFileMap.values());
            }
        }*/
        return ResultView.ok();
    }

    @PostMapping("/add")
    public ResultView add(@Valid @RequestBody MagnetFile magnetFile) {
        this.createBefore(magnetFile);
        magnetFileService.save(magnetFile);
        return ResultView.ok();
    }

    /**
     * 创建前检查
     */
    private void createBefore(MagnetFile magnetFile) {
        //
    }

    @PostMapping("/update")
    public ResultView update(@RequestBody @Valid MagnetFile magnetFile) {
        MagnetFile update = magnetFileService.getById(magnetFile.getId());
        if (update == null) {
            return ResultView.error(ResultEnum.CODE_2, "数据不存在！");
        }
        createBefore(magnetFile);
        magnetFileService.updateById(magnetFile);
        return ResultView.ok();
    }
}
